package com.xiyun.vod.utils;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.xiyun.common.response.exceptions.XiYunException;
import com.xiyun.vod.VodConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class VodUtil {

    /**
     *  初始化信息
     * @return
     */
    private static DefaultAcsClient initVodClient() {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, VodConstant.KEYID, VodConstant.KEYSECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }



    /**
     * 流式上传接口
     *
     * @param file 文件流
     */
    public static String uploadStream(MultipartFile file) {
        //文件原始名称
        String fileName= file.getOriginalFilename();
        System.out.println("文件原始名称:" + fileName);
        // 文件显示名称
        String title = fileName.substring(0,fileName.lastIndexOf("."));
        System.out.println("文件保存至云的名称:" + title);
        //文件流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        }catch (IOException e){
            e.printStackTrace();
        }


        UploadStreamRequest request = new UploadStreamRequest(VodConstant.KEYID, VodConstant.KEYSECRET, title, fileName, inputStream);
        /* 是否使用默认水印(可选)，指定模板组ID时，根据模板组配置确定是否使用默认水印*/
        //request.setShowWaterMark(true);
        /* 自定义消息回调设置，参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
        //request.setUserData(""{\"Extend\":{\"test\":\"www\",\"localId\":\"xxxx\"},\"MessageCallback\":{\"CallbackURL\":\"http://test.test.com\"}}"");
        /* 视频分类ID(可选) */
        //request.setCateId(0);
        /* 视频标签,多个用逗号分隔(可选) */
        //request.setTags("标签1,标签2");
        /* 视频描述(可选) */
        //request.setDescription("视频描述");
        /* 封面图片(可选) */
        //request.setCoverURL("http://cover.sample.com/sample.jpg");
        /* 模板组ID(可选) */
        //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
        /* 工作流ID(可选) */
        //request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
        /* 存储区域(可选) */
        //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
        /* 开启默认上传进度回调 */
        // request.setPrintProgress(true);
        /* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
        // request.setProgressListener(new PutObjectProgressListener());
        /* 设置应用ID*/
        //request.setAppId("app-1000000");
        /* 点播服务接入点 */
        //request.setApiRegionId("cn-shanghai");
        /* ECS部署区域*/
        // request.setEcsRegionId("cn-shanghai");
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        return response.getVideoId();
    }

    /**
     * 删除视频
     * @return DeleteVideoResponse 删除视频响应数据
     * @throws Exception
     */
    public static String removeVideo(String vid) {
        DefaultAcsClient client = initVodClient();

        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(vid);
        DeleteVideoResponse response;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new XiYunException(20001,"删除云端视频失败");
        }
        return response.getRequestId();
    }

    public static void batchRemoveVideos(List<String> list){
        DefaultAcsClient client = initVodClient();

        DeleteVideoRequest request = new DeleteVideoRequest();
        // 将字符串列表转化成特定的字符串数组
        String join = StringUtils.join(list.toArray(), ",");
        //支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(join);
        DeleteVideoResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new XiYunException(20001,"批量删除云端视频失败");
        }
    }
}
